<template>
  <div class="user-manage">
    <el-form
      ref="form"
      :inline="true"
      :model="form"
      class="search-form"
    >
      <el-form-item label="注册时间:">
        <el-date-picker
          v-model="date"
          size="small"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户姓名:">
        <el-input
          size="small"
          v-model="form.name"
          placeholder="用户姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号码:">
        <el-input
          size="small"
          v-model="form.phone"
          placeholder="手机号码"
        ></el-input>
      </el-form-item>
      <br>
      <el-form-item
        label="消费金额:"
        class="half-input"
      >
        <el-input
          v-model="form.money_min"
          size="small"
          placeholder="最小消费金额"
        ></el-input>
        <span class="gutter">至</span>
        <el-input
          v-model="form.money_max"
          size="small"
          placeholder="最大消费金额"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="订单数量:"
        class="half-input"
      >
        <el-input
          v-model="form.count_min"
          size="small"
          placeholder="最小订单数量"
        ></el-input>
        <span class="gutter">至</span>
        <el-input
          v-model="form.count_max"
          size="small"
          placeholder="最大订单数量"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          @click="search(1)"
        >
          查询
        </el-button>
      </el-form-item>
    </el-form>
    <div class="operate-bar" style="margin-top: 22px">
      <el-button
        size="small"
        :disabled="multipleSelection.length === 0"
        @click="dispatchCoupon"
      >
        派发卡劵
      </el-button>
    </div>
    <el-table
      ref="table"
      :data="tableData"
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="nickName"
        label="乘客姓名"
      >
      </el-table-column>
      <el-table-column
        prop="phone"
        label="手机号码"
        width="140"
      >
      </el-table-column>
      <el-table-column
        prop="count"
        label="订单数量"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="money"
        label="消费金额"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="source"
        label="来源"
        width="120"
      >
      </el-table-column>
      <el-table-column
        prop="parent_name"
        label="创建人"
      >
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="注册时间"
        width="175"
      >
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :page-size.sync="form.size"
        :current-page.sync="form.page"
        :total="total"
        :hide-on-single-page="true"
        @current-change="search"
      >
      </el-pagination>
    </div>
    <coupon-dispatch></coupon-dispatch>
  </div>
</template>

<script>
  /**
   * @Author: lifuzhao
   * @Date: 2019-07-14
   * @Project: driver
   */
  import {pageSize} from "../../constant"
  import './userManage.less'
  import couponDispatch from './parts/dispatch'

  export default {
    name: "online-stats"
    , data() {
      return {
        form: {
          page: 1
          , size: pageSize
          , time_begin: ''
          , time_end: ''
          , name: ''
          , phone: ''
          , money_min: ''
          , money_max: ''
          , count_min: ''
          , count_max: ''
        }
        , loading: false
        , date: []
        , total: 0
        , tableData: []
        , multipleSelection: []
      }
    }
    , watch: {
      date(date) {
        let start = ''
          , end = ''
        if (date) {
          [start, end] = date
        }
        this.form.time_begin = start
        this.form.time_end = end
      }
      , $route: {
        deep: true
        , immediate: true
        , async handler() {
          await this.$store.dispatch('getCouponList', {
            params: {page: 1, size: 1000}
            , forceRefresh: false
          })
          this.$bus.$on('user-clear', () => {
            this.$refs.table && this.$refs.table.clearSelection()
          })
          this.search(1)
        }
      }
    }
    , methods: {
      search(page) {
        this.form.page = page
        this.getList()
      }
      , getList() {
        this.$refs.table.clearSelection()
        this.loading = true
        this.$axios.get('/api/v1/users', {
          params: this.form
          , abort: true
        })
          .then(res => {
            let {total = 0, data = []} = res
            this.total = total
            this.tableData = data
          })
          .catch(() => {

          })
          .finally(() => {
            this.loading = false
          })
      }
      , handleSelectionChange(multipleSelection) {
        this.multipleSelection = multipleSelection
      }
      , dispatchCoupon() {
        this.$bus.$emit('coupon-dispatch', this.multipleSelection)
      }
    }
    , components: {
      couponDispatch
    }
  }
</script>
